package com.maimao.staticms.controller;

import com.maimao.core.utils.DateUtils;
import com.maimao.staticms.model.Student;
import freemarker.cache.StringTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author MaoLin Wang
 * @date 2020/12/30 11:21 上午
 */
@Controller
@RequestMapping("/test")
public class TestController {

    @RequestMapping("/t1")
    public String test1(Map<String, Object> m) {
        Student student = new Student();
        student.setAge(1);
        student.setBest(new Student("beast", 23, DateUtils.now(), new BigDecimal(9999.12), null, null));
        student.setBirth(DateUtils.now());
        student.setName("麦猫");
        student.setPrice(new BigDecimal(10.99));
        List<Student> studentList = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            studentList.add(new Student(i + "name", i, DateUtils.now(), new BigDecimal(i), null, null));
        }
        student.setFriends(studentList);

        m.put("Stu", student);
        return "test1";
    }

    public Map t() {
        Map<String, Object> m = new HashMap<>();
        Student student = new Student();
        student.setAge(1);
        student.setBest(new Student("beast", 23, DateUtils.now(), new BigDecimal(9999.12), null, null));
        student.setBirth(DateUtils.now());
        student.setName("麦猫");
        student.setPrice(new BigDecimal(10.99));
        List<Student> studentList = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            studentList.add(new Student(i + "name", i, DateUtils.now(), new BigDecimal(i), null, null));
        }
        student.setFriends(studentList);

        m.put("Stu", student);
        return m;
    }

    @GetMapping("/gen")
    @ResponseBody
    public void genStatis() throws IOException, TemplateException {
        Configuration configuration = new Configuration(Configuration.getVersion());
        String path = this.getClass().getResource("/").getPath();
        configuration.setDirectoryForTemplateLoading(new File(path + "/templates/"));
        Template template = configuration.getTemplate("test1.ftlh");
        Map modelAndView = t();
        String html = FreeMarkerTemplateUtils.processTemplateIntoString(template, modelAndView);

        InputStream inputStream = IOUtils.toInputStream(html);
        FileOutputStream os = new FileOutputStream(new File("/Users/wangmaolin/IdeaProjects/maimao/maimao_server/maimao_parent/maimao_static/src/main/resources/page/test.html"));
        IOUtils.copy(inputStream, os);
    }

    @GetMapping("/gen2")
    @ResponseBody
    public void genStatisstr() throws IOException, TemplateException {
        Configuration configuration = new Configuration(Configuration.getVersion());
        String path = this.getClass().getResource("/").getPath();
        configuration.setDirectoryForTemplateLoading(new File(path + "/templates/"));
        String str = "<!DOCTYPE html>\n" +
                "<html>\n" +
                "<head>\n" +
                "    <meta charset=\"UTF-8\">\n" +
                "    <title>Title</title>\n" +
                "</head>\n" +
                "<body>\n" +
                "<table>\n" +
                "    <tr>\n" +
                "        <td>姓名</td>\n" +
                "        <td>年龄</td>\n" +
                "        <td>价格</td>\n" +
                "        <td>生日</td>\n" +
                "    </tr>\n" +
                "    <#list Stu.friends as stu>\n" +
                "        <tr>\n" +
                "            <td>${stu.name}</td>\n" +
                "            <td>${stu.age}</td>\n" +
                "            <td>${stu.price}</td>\n" +
                "            <td>${stu.birth}</td>\n" +
                "        </tr>\n" +
                "    </#list>\n" +
                "    <br/>\n" +
                "\n" +
                "\n" +
                "</table>\n" +
                "</body>\n" +
                "</html>";
        StringTemplateLoader loader = new StringTemplateLoader();
        loader.putTemplate("template", str);
        configuration.setTemplateLoader(loader);
        Template template = configuration.getTemplate("template", "utf-8");
        Map modelAndView = t();
        String html = FreeMarkerTemplateUtils.processTemplateIntoString(template, modelAndView);

        InputStream inputStream = IOUtils.toInputStream(html);
        FileOutputStream os = new FileOutputStream(new File("/Users/wangmaolin/IdeaProjects/maimao/maimao_server/maimao_parent/maimao_static/src/main/resources/page/test.html"));
        IOUtils.copy(inputStream, os);
        inputStream.close();
        os.close();
    }
}
